package cn.cmcc.online.smsapi.sdk.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;

import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.log.LogManager;

public class SdkCache {
	
	    public   Map<String, JSONObject> cacheBubbleData = new HashMap<String, JSONObject>();
	    public   Map<String, JSONObject> effectiveBubbleData1 = new HashMap<String, JSONObject>();
	    public   Map<String, JSONArray> effectiveBubbleData = new HashMap<String, JSONArray>();
	    public   HashSet<String> invalidBubbleData = new HashSet<String>();
	    public   HashSet<String> inQueueBubbleData = new HashSet<String>();
	    
	    
        public final Map<String, JSONObject> cacheRichBubbleData = new HashMap<String, JSONObject>();
        public final Map<String, JSONObject> effectiveRichBubbleData = new HashMap<String, JSONObject>();
        public final HashSet<String> invalidRichBubbleData = new HashSet<String>();
        public final HashSet<String> inQueueRichBubbleData = new HashSet<String>();
        
        /*SDK-514 zhaojiangwei 20160630 start*/
        public  final Map<String, Map<String, Object>> baseRefsValue = new HashMap<String, Map<String, Object>>();
        /*SDK-514 zhaojiangwei 20160630 end*/
        
        /* SDK-383 xusongzhou 20160414 start */
        public  final Map<String, JSONObject> cacheRecognisedValue = new HashMap<String, JSONObject>();
        public  final Map<String, JSONObject> effectiveRecognisedValue = new HashMap<String, JSONObject>();
        public  final HashSet<String> invalidRecognisedValue = new HashSet<String>();
        public  final HashSet<String> inQueueRecognisedValue = new HashSet<String>();
        /* SDK-383 xusongzhou 20160414 end */
        
        /*MEIZU-1142 zhaojiangwei 20160908 start*/
        public final Map<String, JSONObject> cacheUrlsData = new HashMap<String, JSONObject>();
        public final Map<String, JSONObject> effectiveUrlsData = new HashMap<String, JSONObject>();
        public final HashSet<String> inQueueUrlsData = new HashSet<String>();
        /*MEIZU-1142 zhaojiangwei 20160908 end*/
        
        /*RM-1279 mobaohua 20170314 start*/
        public final Map<String, JSONObject> cacheUrlPreviewData = new HashMap<String, JSONObject>();
        public final Map<String, JSONObject> effectiveUrlPreviewData = new HashMap<String, JSONObject>();
        public final HashSet<String> inQueueUrlPreviewData = new HashSet<String>();
        /*RM-1279 mobaohua 20170314 end*/
        /*RM-1467 mobaohua 20170406 start*/
        public final Map<String, JSONObject> cacheMovieMessageData = new HashMap<String, JSONObject>();
        public final Map<String, JSONObject> effectiveMovieMessageData = new HashMap<String, JSONObject>();
        public final HashSet<String> inQueueMovieMessageData = new HashSet<String>();
        /*RM-1467 mobaohua 20170406 end*/
        /*SDK-765 mobaohua 20170419 start*/
        public final Map<String, JSONObject> cacheExpressInfoData = new HashMap<String, JSONObject>();
        public final Map<String, JSONObject> effectiveExpressInfoData = new HashMap<String, JSONObject>();
        public final HashSet<String> inQueueExpressInfoData = new HashSet<String>();
        /*SDK-765 mobaohua 20170419 end*/
        
        //global cache
	    private static  HashMap<String, SdkCache> sdkCacheMap  = new HashMap<String, SdkCache>();
	    
	    public   static SdkCache getSdkCache(String key){
	    	return sdkCacheMap.get(key);
	    }
        public   static SdkCache createSdkCache(String key){
	    	 
	    	SdkCache cache = getSdkCache(key);
	    	if(cache == null){
	    		cache = new SdkCache();
	    		sdkCacheMap.put(key, cache);
	    	}
	    	return cache;
	    }
        public   static void delSdkCache(String key){
        	SdkCache cache = getSdkCache(key);
        	if(cache != null){
        		cache.clearSdkCache();
        		cache.clearSdkRichCache();
        	}
	    }
        public static void putCacheBubbleData(String key, Map<String, JSONObject> bubbleData ){
        	if(bubbleData == null){
        		return;
        	}
        	SdkCache cache = createSdkCache(key);
        	cache.cacheBubbleData.putAll(bubbleData);
        }
        public static void putCacheRichBubbleData(String key, Map<String, JSONObject> bubbleData ){
        	if(bubbleData == null){
        		return;
        	}
        	/* SDK-402 huangzhiqiang 20160420 start */
        	SdkCache cache = createSdkCache(key);
        	cache.cacheRichBubbleData.putAll(bubbleData);
        	/* SDK-402 huangzhiqiang 20160420 end */
        }
        public void clearSdkRichCache(){
        	try{
	    		 cacheRichBubbleData.clear();
	    		 effectiveRichBubbleData.clear();
	    		 invalidRichBubbleData.clear();
	    		 inQueueRichBubbleData.clear();
        	}catch(Throwable e){
//        		e.printStackTrace();
                LogManager.e(Constant.TAG, "clearSdkRichCache: ", e);

        	}
        }
        
        public void clearUrlDataCache(){
        	try{
	    		 cacheUrlsData.clear();
	    		 effectiveUrlsData.clear();
	    		 inQueueUrlsData.clear();
        	}catch(Throwable e){
//        		e.printStackTrace();
                LogManager.e(Constant.TAG, "clearUrlDataCache: ", e);

        	}
        }
        public void clearSdkCache(){
        	try{
	        	 cacheBubbleData.clear();
	    		 effectiveBubbleData1.clear();
	    		 effectiveBubbleData.clear();
	    		 invalidBubbleData.clear();
	    		 inQueueBubbleData.clear();
                 /* SDK-383 xusongzhou 20160414 start */
                 cacheRecognisedValue.clear();
                 effectiveRecognisedValue.clear();
                 invalidRecognisedValue.clear();
                 inQueueRecognisedValue.clear();
                 /* SDK-383 xusongzhou 20160414 end */
        	}catch(Throwable e){
//        		e.printStackTrace();
                LogManager.e(Constant.TAG, "clearSdkCache: ", e);

        	}
        }

        /**
         * 清除指定msgId的所有内存缓存
         */
        public void clearSdkCache(String msgId) {
            try {
                /* SDK-591 huangzhiqiang 20160815 start */
                clearParseDataCache(msgId);
                /* SDK-591 huangzhiqiang 20160815 end */
                invalidRichBubbleData.remove(msgId);
                inQueueRichBubbleData.remove(msgId);
                inQueueBubbleData.remove(msgId);
    
                /* SDK-383 xusongzhou 20160414 start */
                invalidRecognisedValue.remove(msgId);
                inQueueRecognisedValue.remove(msgId);
                /* SDK-383 xusongzhou 20160414 end */
            } catch (Throwable e) {
                // TODO: handle Throwable
            }
        }
        public static void clearAllSdkCache(String msgId){
        	try{
        		Set<Entry<String,SdkCache>> set = sdkCacheMap.entrySet();
    			for(Entry<String,SdkCache> entry : set){
    				entry.getValue().clearSdkCache(msgId);
    			}
        	}catch(Throwable e){
        	}
        }
        
        /* SDK-528 zhaojiangwei 20160711 start*/
		public static void clearRecogniseSdkCache() {
			try {
				Set<Entry<String, SdkCache>> set = sdkCacheMap.entrySet();
				for (Entry<String, SdkCache> entry : set) {
					entry.getValue().cacheRecognisedValue.clear();
					entry.getValue().effectiveRecognisedValue.clear();
					entry.getValue().invalidRecognisedValue.clear();
					entry.getValue().inQueueRecognisedValue.clear();
				}
			}
			catch (Throwable e) {
				
			}
		}
		
		public static void clearRecogniseSdkCache(String phone) {
			try {
				SdkCache recogniseCache = sdkCacheMap.get(phone);
				if (recogniseCache!=null) {
					recogniseCache.cacheRecognisedValue.clear();
					recogniseCache.effectiveRecognisedValue.clear();
					recogniseCache.invalidRecognisedValue.clear();
					recogniseCache.inQueueRecognisedValue.clear();
				}
			}
			catch (Throwable e) {
				
			}
		}

        /* SDK-591 huangzhiqiang 20160815 start */
        /**
         * 清除指定msgId的所有识别结果缓存
         */
        public void clearParseDataCache(String msgId) {
            try {
                cacheRichBubbleData.remove(msgId);
                effectiveRichBubbleData.remove(msgId);
                cacheBubbleData.remove(msgId);
                effectiveBubbleData.remove(msgId);
                effectiveBubbleData1.remove(msgId);
    
                /* SDK-383 xusongzhou 20160414 start */
                cacheRecognisedValue.remove(msgId);
                effectiveRecognisedValue.remove(msgId);
                /* SDK-383 xusongzhou 20160414 end */
                
                effectiveUrlsData.remove(msgId);
            } catch (Throwable e) {
                // TODO: handle Throwable
            }
        }

        /**
         * 清除指定msgId的所有识别结果缓存
         */
        public static void clearAllParseDataCache(String msgId) {
            try {
                Set<Entry<String, SdkCache>> set = sdkCacheMap.entrySet();
                for (Entry<String, SdkCache> entry : set) {
                    entry.getValue().clearParseDataCache(msgId);
                }
            } catch (Throwable e) {
                // TODO: handle Throwable
            }
        }
        /* SDK-591 huangzhiqiang 20160815 end */
        /* SDK-528 zhaojiangwei 20160711 end*/
}
